require_relative 'npi'
require_relative 'pdu'
require_relative 'ton'

module SmppClient

  module Pdu

    # This message is sent by the SMSC to the ESME, when the SMSC has detected that a particular
    # mobile subscriber has become available and a delivery pending flag had been set for that
    # subscriber from a previous data_sm operation.
    #
    # It may be used for example to trigger a data content ‘Push’ to the subscriber from a WAP Proxy
    # Server.
    #
    # Note: There is no alert_notification_resp PDU.

    class AlertNotification < Pdu

      # Max  _source_addr_ and _esme_addr_ fields length - trailing C-Octet 00 octet.
      MAX_ADDRESS_LENGTH = 65 - 1

      attr_accessor :source_addr_ton, :source_addr_npi, :source_addr,
                  :esme_addr_ton, :esme_addr_npi, :esme_addr

      def initialize(byte_buffer = nil)
        if byte_buffer == nil
          super(CommandId::ALERT_NOTIFICATION)
          @source_addr_ton = Ton::UNKNOWN
          @source_addr_npi = Npi::UNKNOWN
          @source_addr = ''
          @esme_addr_ton = Ton::UNKNOWN
          @esme_addr_npi = Npi::UNKNOWN
          @esme_addr = ''
        else
          super(byte_buffer)
          parse_body(byte_buffer)
          parse_tlvs(byte_buffer)
        end
      end

      def parse_body(byte_buffer)
        @source_addr_ton = byte_buffer.remove_byte
        @source_addr_npi = byte_buffer.remove_byte
        @source_addr = byte_buffer.remove_cstring
        @esme_addr_ton = byte_buffer.remove_byte
        @esme_addr_npi = byte_buffer.remove_byte
        @esme_addr = byte_buffer.remove_cstring
      end

      def body_bytes
        buffer = ByteBuffer.new
        buffer.append_byte(@source_addr_ton)
        buffer.append_byte(@source_addr_npi)
        buffer.append_cstring(@source_addr)
        buffer.append_byte(@esme_addr_ton)
        buffer.append_byte(@esme_addr_npi)
        buffer.append_cstring(@esme_addr)
        return buffer.bytes
      end

    end

  end

end